from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms

writer = SummaryWriter("logs")
img = Image.open(r'F:\86135\Documents\DL_torch\data\train\ants_image\5650366_e22b7e1065.jpg')

trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
writer.add_image("ToTensor", img_tensor)

# 归一化Normalize
print(img_tensor[0][0][0])
trans_norm = transforms.Normalize([1, 3, 55], [3, 2, 1])
img_norm = trans_norm(img_tensor)
print(img_norm[0][0][0])
writer.add_image("Normalize", img_norm, 2)

# resize
print(img.size)
trans_resize = transforms.Resize((512, 512))
# img为PIL类型 ->resize -> img_resize PIL
img_resize = trans_resize(img)

# img为tensor类型 img_resize PIL -> tensor ->img_resize tensor
img_resize = trans_totensor(img_resize)
writer.add_image("Resize", img_resize, 0)
print(img_resize)

# compose   - resize -2 整体缩放只改变最小边最长边的大小关系

trans_resize_2 = transforms.Resize(512)
trans_compose = transforms.Compose([trans_resize_2, trans_totensor])
img_resize_2 = trans_compose(img)
writer.add_image("Resize", img_resize_2, 1)

# RandomCrop 随机裁剪
trans_randomc = transforms.RandomCrop((240, 120))
trans_compse_2 = transforms.Compose([trans_randomc, trans_totensor])
for i in range(10):
    img_crop = trans_compse_2(img)
    writer.add_image("RandomCrop", img_crop, i)

writer.close()
